package rpg.uiView
{
	import flash.display.Shape;
	import flash.geom.Point;

	import ui.Component;

	/**
	 * 模拟出一个物品冷却状态
	 * @author hyy
	 *
	 */
	public class CoolAnimation extends Component
	{
		private var animation : Shape;
		/**
		 * 半径
		 */
		private var radius : int;
		/**
		 * 颜色
		 */
		private var color : int = 0x000000;
		/**
		 * cd持续时间
		 */
		private var duration : int;
		private var elapsedTime : int;

		public function CoolAnimation(width : int = 40 , height : int = 40)
		{
			super();
			this.radius = Math.sqrt(width * width * 0.25 + height * height * 0.25);
			this.setSize(width , height);
			animation = new Shape();
			addChild(animation);
			animation.x = width * .5;
			animation.y = height * .5;
			var tmpMask : Shape = new Shape();
			tmpMask.graphics.beginFill(0);
			tmpMask.graphics.drawRect(0 , 0 , width , height);
			tmpMask.graphics.endFill();
			this.animation.mask = tmpMask;
			this.addChild(tmpMask);
		}

		override public function init() : void
		{
			this.mouseChildren = false;
			this.mouseEnabled = false;
		}

		public function update(delay : int) : void
		{
			elapsedTime += delay;
			percent = elapsedTime / duration;
		}

		public function createCD(value : int) : void
		{
			duration = value;
			elapsedTime = 0;
		}

		public function get isCD() : Boolean
		{
			return elapsedTime < duration;
		}

		/**
		 * 设置冷却进度百分比
		 * @param value
		 *
		 */
		private function set percent(value : Number) : void
		{
			if(value > 1)
				value = 1;
			var roation : int = value * 360;
			animation.graphics.clear();
			if(elapsedTime >= duration)
				return;
			animation.graphics.beginFill(color , 0.5);
			animation.graphics.moveTo(0 , 0);
			for(var i : int = roation ; i < 360 ; i++)
			{
				var angle : Number = Math.PI / 180 * i;
				var point : Point = Point.polar(radius , angle);
				animation.graphics.lineTo(point.x , point.y);
			}
			animation.graphics.endFill();
		}

		public function setColor(value : int) : void
		{
			this.color = value;
		}
	}
}